package com.questionnaire.core.shiro.factory;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.questionnaire.common.base.ShiroUser;
import com.questionnaire.modular.system.dao.LoginPermissionMapper;
import com.questionnaire.modular.system.dao.MenuMapper;
import com.questionnaire.modular.system.dao.UserMapper;
import com.questionnaire.modular.system.model.User;
import com.questionnaire.utils.Convert;
import com.questionnaire.utils.SpringContextHolder;

import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@DependsOn("springContextHolder")
@Transactional(readOnly = true)
public class ShiroFactroy implements IShiro {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginPermissionMapper loginPermissionMapper;

    @Autowired
    private MenuMapper menuMapper;

    public static IShiro me() {
        return SpringContextHolder.getBean(IShiro.class);
    }

    @Override
    public User user(String userName) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,userName);
        //解决用户名重名的问题,返回两项的问题
        List<User> users = userMapper.selectList(wrapper);
        if (users.size() > 1){
            User user1 = new User();
            for (User user : users) {
                //如果是正常状态 直接返回此项
                if (user.getEnable() == 0) {
                    return user;
                }
            }
            return users.get(0);
        }
        User user = userMapper.selectOne(wrapper);
   //  账号不存在
     if (null == user) {
     throw new CredentialsException();
     }
        return user;
    }

    @Override
    public ShiroUser shiroUser(User user) {
        ShiroUser shiroUser = new ShiroUser();

        shiroUser.setId(user.getId());
        shiroUser.setAccount(user.getUserName());
       shiroUser.setName(user.getRealName());

        Integer[] roleArray = Convert.toIntArray(user.getRoleId());
        List<Integer> roleList = new ArrayList<Integer>();
        List<String> roleNameList = new ArrayList<String>();

        for (int roleId : roleArray) {
            roleList.add(roleId);
            roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
        }
        shiroUser.setRoleList(roleList);
        shiroUser.setRoleNames(roleNameList);
        //根据角色来判断用户权限
        int[] flags= {0,0,0,0};
        String userAuth="user";
        String orgId="";
        for(String roleName:roleNameList){
            if(roleName.indexOf("普通")>=0){
                flags[3]=1;
            }
            if(roleName.indexOf("后台")>=0){
                flags[2]=1;
            }
            if(roleName.indexOf("超级")>=0){
                flags[1]=1;
            }
        }
        //然后根据遍历结果,判断最高权限
        if(1==flags[1]){
            userAuth="admin";
        }else if(1==flags[2]){
            userAuth="org";
        }else if(1==flags[3]){
            userAuth="user";

        }else{
            userAuth="user";
        }

        shiroUser.setUserAuth(userAuth);
        return shiroUser;
    }

    @Override
    public List<String> findPermissionsByRoleId(Integer roleId) {
        return menuMapper.getResUrlsByRoleId(roleId);
    }

    @Override
    public String findRoleNameByRoleId(Integer roleId) {
        return ConstantFactory.me().getSingleRoleTip(roleId);
    }

    @Override
    public SimpleAuthenticationInfo info(ShiroUser shiroUser, User user, String realmName) {
        String credentials = user.getPassWord();
        // 密码加盐处理
        String source = user.getSalt();
        ByteSource credentialsSalt = new Md5Hash(source);
        return new SimpleAuthenticationInfo(shiroUser, credentials, credentialsSalt, realmName);
    }

}
